﻿using Coldairarrow.Business.TD;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.TD;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.TD
{
    [Route("/TD/[controller]/[action]")]
    public class TD_OutStorDetailController : BaseApiController
    {
        #region DI

        public TD_OutStorDetailController(ITD_OutStorDetailBusiness tD_OutStorDetailBus,IOperator @op)
        {
            _tD_OutStorDetailBus = tD_OutStorDetailBus;
            _Op = @op;
        }
        IOperator _Op { get; }
        ITD_OutStorDetailBusiness _tD_OutStorDetailBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<TD_OutStorDetail>> GetDataListAsyncs(PageInput<ConditionDTO> input)
        {
         
            return await _tD_OutStorDetailBus.GetDataListAsyncs(input);
        }
        [HttpPost]
        public async Task<PageResult<TD_OutStorDetailDTO>> GetDataListAsyncDtoList(PageInput<TD_OutStorDetailConditonDTO> input) {
            return await _tD_OutStorDetailBus.GetDataListAsyncDtoList(input);
        }
        [HttpPost]
        public async Task<TD_OutStorDetail> GetTheData(IdInputDTO input)
        {
            return await _tD_OutStorDetailBus.GetTheDataAsync(input.id);
        }


        [HttpPost]
        public async Task<List<TD_OutStorDetailDTO>> GetDataListAsynclist()
        {
            var res = await _tD_OutStorDetailBus.GetDataListAsynclist();
            return res;
        }
        /// <summary>
        ///导出出库明细  通用  朱庆龙     2022/3/23
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<IActionResult> GetExportList(PageInput<TD_OutStorDetailConditonDTO> input)
        {
            var res = await _tD_OutStorDetailBus.GetDataListAsyncDtoList(input);
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet s1 = book.CreateSheet("出库明细");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("出库单号");
            r1.CreateCell(1).SetCellValue("出库类型");
            r1.CreateCell(2).SetCellValue("出库时间");
            r1.CreateCell(3).SetCellValue("出库仓库");
            r1.CreateCell(4).SetCellValue("货区/货架/货位");
            r1.CreateCell(5).SetCellValue("物料编号");
            r1.CreateCell(6).SetCellValue("物料名称");
            r1.CreateCell(7).SetCellValue("规格");
            r1.CreateCell(8).SetCellValue("出库数量");
            r1.CreateCell(9).SetCellValue("制单人");
            for (int i = 0; i < res.Data.Count; i++)
            {
                IRow rt = s1.CreateRow(i + 1);
                rt.CreateCell(0).SetCellValue(res.Data[i].Codeinfo);
                rt.CreateCell(1).SetCellValue(res.Data[i].Name);
                rt.CreateCell(2).SetCellValue(Convert.ToDateTime(res.Data[i].RecordTime).ToString("yyyy-MM-dd"));
                rt.CreateCell(3).SetCellValue(res.Data[i].StorIdName);
                rt.CreateCell(4).SetCellValue(res.Data[i].AreaIdName+"/"+res.Data[i].RackIdName+"/"+res.Data[i].LocalIdName);
                rt.CreateCell(5).SetCellValue(res.Data[i].Code);
                rt.CreateCell(6).SetCellValue(res.Data[i].MaterialIdName);
                rt.CreateCell(7).SetCellValue(res.Data[i].Spec);
                rt.CreateCell(8).SetCellValue(Convert.ToDouble(res.Data[i].OutNum));
                rt.CreateCell(9).SetCellValue(res.Data[i].RealName);

            }
            //输出的文件名称
            string fileName = "出库明细" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            book.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);
        }
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(TD_OutStorDetail data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _tD_OutStorDetailBus.AddDataAsync(data);
            }
            else
            {
                await _tD_OutStorDetailBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _tD_OutStorDetailBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}